﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Serialization;
using System.IO;

[Serializable]
public class Order
{
    public int OrderId { get; set; }
    public string Customer { get; set; }
    public List<OrderDetails> OrderDetails { get; set; } = new List<OrderDetails>();
    public decimal TotalAmount => CalculateTotalAmount();

    private decimal CalculateTotalAmount()
    {
        decimal total = 0;
        foreach (var detail in OrderDetails)
        {
            total += detail.Amount;
        }
        return total;
    }

    public override bool Equals(object obj)
    {
        if (obj == null || GetType() != obj.GetType()) return false;
        var other = (Order)obj;
        return OrderId == other.OrderId;
    }

    public override int GetHashCode() => OrderId.GetHashCode();

    public override string ToString() => $"订单号: {OrderId}, 客户: {Customer}, 总金额: {TotalAmount:C}";
}

[Serializable]
public class OrderDetails
{
    public string ProductName { get; set; }
    public int Quantity { get; set; }
    public decimal Price { get; set; }
    public decimal Amount => Quantity * Price;

    public override bool Equals(object obj)
    {
        if (obj == null || GetType() != obj.GetType()) return false;
        var other = (OrderDetails)obj;
        return ProductName == other.ProductName;
    }

    public override int GetHashCode() => ProductName.GetHashCode();

    public override string ToString() => $"{ProductName}, 数量: {Quantity}, 单价: {Price:C}, 总金额: {Amount:C}";
}

public class OrderService
{
    private List<Order> orders = new List<Order>();

    public void AddOrder(Order order)
    {
        if (orders.Contains(order)) throw new Exception("订单已存在");
        orders.Add(order);
    }

    public void RemoveOrder(int orderId)
    {
        var order = orders.FirstOrDefault(o => o.OrderId == orderId);
        if (order == null) throw new Exception("订单不存在");
        orders.Remove(order);
    }

    public void UpdateOrder(int orderId, string customer)
    {
        var order = orders.FirstOrDefault(o => o.OrderId == orderId);
        if (order == null) throw new Exception("订单不存在");
        order.Customer = customer;
    }

    public List<Order> QueryOrders(Func<Order, bool> predicate) =>
        orders.Where(predicate).OrderByDescending(o => o.TotalAmount).ToList();

    public void SortOrders(Func<Order, object> orderBy) =>
        orders = orders.OrderBy(orderBy).ToList();

    public void Export(string filePath)
    {
        XmlSerializer serializer = new XmlSerializer(typeof(List<Order>));
        using (FileStream fs = new FileStream(filePath, FileMode.Create))
        {
            serializer.Serialize(fs, orders);
        }
    }

    public void Import(string filePath)
    {
        XmlSerializer serializer = new XmlSerializer(typeof(List<Order>));
        using (FileStream fs = new FileStream(filePath, FileMode.Open))
        {
            var imported = (List<Order>)serializer.Deserialize(fs);
            foreach (var order in imported)
            {
                if (!orders.Contains(order))
                {
                    orders.Add(order);
                }
            }
        }
    }
}

public class Program
{
    public static void Main(string[] args)
    {
        try
        {
            var orderService = new OrderService();

            var order1 = new Order { OrderId = 1, Customer = "张三" };
            order1.OrderDetails.Add(new OrderDetails { ProductName = "商品1", Quantity = 2, Price = 50 });
            order1.OrderDetails.Add(new OrderDetails { ProductName = "商品2", Quantity = 1, Price = 200 });
            orderService.AddOrder(order1);

            var order2 = new Order { OrderId = 2, Customer = "李四" };
            order2.OrderDetails.Add(new OrderDetails { ProductName = "商品3", Quantity = 3, Price = 100 });
            order2.OrderDetails.Add(new OrderDetails { ProductName = "商品4", Quantity = 1, Price = 150 });
            orderService.AddOrder(order2);

            Console.WriteLine("添加订单完成");

            var result = orderService.QueryOrders(o => o.Customer == "张三");
            foreach (var order in result) Console.WriteLine(order);

            orderService.UpdateOrder(1, "王五");
            Console.WriteLine("修改订单完成");

            orderService.SortOrders(o => o.TotalAmount);
            foreach (var order in orderService.QueryOrders(o => true)) Console.WriteLine(order);

            string exportPath = "orders_export.xml";
            orderService.Export(exportPath);
            Console.WriteLine($"导出完成：{exportPath}");

            orderService = new OrderService();
            orderService.Import(exportPath);
            foreach (var order in orderService.QueryOrders(o => true)) Console.WriteLine(order);

            orderService.RemoveOrder(1);
            Console.WriteLine("删除订单完成");
        }
        catch (Exception ex)
        {
            Console.WriteLine($"错误: {ex.Message}");
        }
    }
}
